/** @format */

// import Vue from 'vue'
import { createStore } from 'vuex'
// import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'

// Vue.use(Vuex)
const store = createStore({
  state: {
    fullscreenFlag: true, //全屏的标识
    isLoading: false, //接口loading加载动画
    Activepath: '', //当前的路由
    visited: '',
    userId: '', //用户id
    theme: '', //主题色
    childrenMenu: [], //二级菜单
    AllMenu: [], //所有的菜单
    powerMenu: [], //权限菜单
    setAndHistory: [], //设置和历史
    IsSetting: [], //用于存放设置或者历史的数据，在菜单中使用，处理为单个数据
    currentMenu: '', // 当前选中的一级菜单
    fields: [], // 用于存放设置或者历史中的字段
    settingFields: {}, // 系统设置中的字段
  },

  getters: {
    settingFields: (state: { settingFields: any }) => state.settingFields,
  },
  mutations: {
    SettingFields(state: { settingFields: any }, value: any) {
      state.settingFields = value
    },
    setFiels(state: { fields: any }, value: any) {
      state.fields = value
    },
    // 设置主题色
    setTheme(state: { theme: any }, value: any) {
      state.theme = value
    },
    // 设置用户id
    setUserId(state: { userId: any }, value: any) {
      state.userId = value
    },
    // 当前的路由
    Tage(state: { powerMenu: any[]; currentMenu: any; childrenMenu: any; Activepath: any }, value: any) {
      console.log('value', value);
      console.log('state',state);
      
      if (state.powerMenu) {
        state.powerMenu.map((item: { children: any[] }) => {          
          if (item.children) {
            item.children.map((item1: { href: any; pid: any }) => {
              if (item1.href == value) {
                state.currentMenu = item1.pid
                state.childrenMenu = item.children
              }
            })
          }
        })
      }
      
      state.Activepath = value
    },
    // 获取当前选中的一级菜单
    getCurrentMenu(state: { currentMenu: any }, value: string) {
      state.currentMenu = value
      localStorage.setItem('menuId', value)
    },
    // 获取所有的二级菜单
    getAllMenu(state: { AllMenu: never[] }, tree: any) {
      state.AllMenu = []
      state.AllMenu = tree
    },
    getChildrenMenu(state: { childrenMenu: never[] }, tree: any) {
      state.childrenMenu = []
      state.childrenMenu = tree
    },
    getPowerMenu(state: { powerMenu: any }, tree: any) {
      state.powerMenu = tree
    },
    changfullscreenFlag(state: { fullscreenFlag: boolean }) {
      // 监听浏览器是否全屏
      window.onresize = function () {
        var isFull =
          document.fullscreenElement ||
          document.mozFullScreenElement ||
          document.webkitFullscreenElement ||
          document.msFullscreenElement
        if (isFull) {
          state.fullscreenFlag = false
        } else {
          state.fullscreenFlag = true
        }
      }
      state.fullscreenFlag = !state.fullscreenFlag
      if (state.fullscreenFlag) {
        //退出全屏
        document.exitFullscreen()
      } else {
        //全屏
        var docElm = document.documentElement
        if (docElm.requestFullscreen) {
          docElm.requestFullscreen()
        } else if (docElm.webkitRequestFullScreen) {
          docElm.webkitRequestFullScreen()
        } else if (docElm.mozRequestFullScreen) {
          docElm.mozRequestFullScreen()
        }
      }
    },
    getvisited(state: { visited: any }, value: any) {
      console.log('getvisited', value);
      state.visited = value
    },
  },
  actions: {
    Fullscreen(context: { commit: (arg0: string) => void }) {
      //接受来自组件中的方法
      context.commit('changfullscreenFlag')
    },
    changeSetAndHistory(context: { state: { IsSetting: {}; setAndHistory: { id: string }[] } }, value: string) {
      context.state.IsSetting = {} //置空数组
      // context.commit('getSetAndHistory');
      if (value == '系统设置') {
        context.state.IsSetting = context.state.setAndHistory[0]
        localStorage.setItem('menuId', context.state.setAndHistory[0].id)
      } else if (value == '历史记录') {
        context.state.IsSetting = context.state.setAndHistory[1]
        localStorage.setItem('menuId', context.state.setAndHistory[1].id)
      }
    },
    getSetAndHistory(context: { state: { setAndHistory: any; powerMenu: any[] } }) {
      context.state.setAndHistory = context.state.powerMenu.filter(
        (item: { name: string }) => item.name == '系统设置'
      )
      // context.state.setAndHistory = [...context.state.powerMenu.slice(-1)];
    },
    // 根据子菜单来获取对应的父级菜单
    getParentMenu({ commit }: any, value: any) {
      commit('Tage', value)
    },
    getSettingFields({ commit }: any, value: any) {
      commit('SettingFields', value)
    },
  },
  modules: {},
  plugins: [
    createPersistedState({
      storage: window.sessionStorage, // 或者 localStorage
    }),
  ],
})

export default store
